package com.cjm.one.coupon.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.utils.AuthCustomerContextHolder;
import com.cjm.one.coupon.service.CouponCalculationService;
import com.cjm.one.coupon.service.CouponInfoService;
import com.cjm.one.model.coupon.dto.OrderCalculateDTO;
import com.cjm.one.model.coupon.entity.CouponInfo;
import com.cjm.one.model.coupon.vo.CouponInfoVo;
import com.cjm.one.model.coupon.vo.GoodsDiscountVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "4-app-优惠券信息", description = "优惠券信息")
@RequestMapping("/app/coupon/info")
public class AppCouponInfoController {

    @Resource
    private CouponInfoService couponInfoService;
    @Resource
    private CouponCalculationService couponCalculationService;

    @GetMapping("/list")
    @Operation(summary = "查询所有优惠券信息列表并判断是否被当前用户领取过")
    public Result<Page<CouponInfoVo>> getList(@RequestParam Long current,
                                            @RequestParam Long size ,
                                            @RequestParam(name = "rangeType", required = false) Integer rangeType) {
        log.debug("查询优惠券信息列表, 当前页码:{}, 页面大小:{}", current, size);
        Long customerId = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        Page<CouponInfoVo> page = couponInfoService.getCouponInfoAppPage(current, size, rangeType,customerId);
        return Result.ok(page);
    }
    @PostMapping("/calculate")
    public Result<List<GoodsDiscountVO>> calculateBestDiscount(
            @RequestBody OrderCalculateDTO calculateDTO) {
        return Result.ok(
                couponCalculationService.calculateOptimalDiscount(calculateDTO)
        );
    }

}
